﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;

namespace Yxinmt.Controls
{
    public class Image : BaseControl, IControl
    {
        private System.Windows.Controls.Image _Image { get; set; }
        bool _IsGif { get; set; }

        public Image(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;
            _IsFactory = isFactory;
            if (_IsFactory)
            {
                _FactoryPropertyList = new List<FactoryProperty>();
                _ControlType = typeof(System.Windows.Controls.Image);
            }

            _IsGif = Property.GetValue<bool>("IsGif");

            _Image = _IsGif ? new GifImage(GetImagePath(Property.GetStringValue("ImageUrl"))) : new System.Windows.Controls.Image();
            this.Element = _Image;

            InitSet();
            this.SetProperty();
            this.SetControlProperty(false, false);
        }

        private void SetProperty()
        {
            this.SetDefaultValue("Stretch", "UniformToFill");
        }

        public System.Windows.Controls.Image GetImage()
        {
            return _Image;
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.Image c = _Image;
            switch (name)
            {
                case "name": c.Name = string.Concat("img_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Height":
                case "ImageHeight": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "Width":
                case "ImageWidth": c.Width = Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "ImageUrl":
                    {
                        if (_IsGif) break;
                        c.Source = this.GetImage((string)value); SetFactoryProperty("Source", c.Source);
                        break;
                    }
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Stretch": c.Stretch = this.GetStretch((string)value); SetFactoryProperty("Stretch", c.Stretch); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "Value": SetImageSource((byte[])value); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
            }
        }

        public void SetStretch(string stretch)
        {
            _Image.Stretch = this.GetStretch(stretch);
        }

        void SetImageSource(byte[] bytes)
        {
            _Image.Source = Utility.Common.GetImageSource(bytes);
        }

        public void SetImageSource(System.Windows.Media.ImageSource imageSource)
        {
            _Image.Source = imageSource;
        }

        public System.Windows.Media.ImageSource GetImageSource()
        {
            return _Image.Source;
        }


        public System.Windows.Media.ImageSource GetGrayImageSource()
        {
            return _Image.Source;
        }
       
        protected override void SetFactoryProperty(string name, object value)
        {
            if (!_IsFactory) return;

            switch (name)
            {
                case "Margin": this.AddFactoryProperty(System.Windows.Controls.Image.MarginProperty, value); break;
                case "Name": this.AddFactoryProperty(System.Windows.Controls.Image.NameProperty, value); break;
                case "Height": this.AddFactoryProperty(System.Windows.Controls.Image.HeightProperty, value); break;
                case "Width": this.AddFactoryProperty(System.Windows.Controls.Image.WidthProperty, value); break;
                case "Source": this.AddFactoryProperty(System.Windows.Controls.Image.SourceProperty, value); break;
                case "Stretch": this.AddFactoryProperty(System.Windows.Controls.Image.StretchProperty, value); break;
                case "VerticalAlignment": this.AddFactoryProperty(System.Windows.Controls.Image.VerticalAlignmentProperty, value); break;
                case "HorizontalAlignment": this.AddFactoryProperty(System.Windows.Controls.Image.HorizontalAlignmentProperty, value); break;
                case "Visibility": this.AddFactoryProperty(System.Windows.Controls.Image.VisibilityProperty, value); break;
            }
        }

        public override void EventLoad()
        {
            bool isClick = Property.GetValue<bool>("IsClick");
            bool isDoubleClick = Property.GetValue<bool>("IsDoubleClick");

            if (isClick || isDoubleClick)
            {
                ClickAction = (sender, e, ctl) =>
                {
                    if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowClick");
                    else this._Window.EventInvoke(sender, e, this);
                };

                DoubleClickAction = (sender, e, ctl) =>
                {
                    if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowDoubleClick");
                    else this._Window.EventInvoke(sender, e, this);
                };

                BindClick(isClick, isDoubleClick);
            }
        }

        public override void DataLoad()
        {
            base.DataLoad();

            if (_IsGif) ((GifImage)this._Image).StartAnimate();
        }
    }
}
